package com.project.domain.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.domain.entity.BizReviewScheme;
import com.project.domain.entity.BizReviewStandard;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by cw13031$ on 2025/2/13.
 * Description:
 */
@Data
public class BizReviewSchemeVo4EditPage{

    private String id;
    private String name;
    private String fullMark;
    private ArrayList<Lv1> items;

    private LocalDateTime createDate;
    private String createUserid;
    private String lastEditUserid;
    private LocalDateTime lastEditDate;
    private String createUsername;
    private String lastEditUsername;


    /**
     * 将前端格式化数据，转为反人类的后端期望格式
     */
    public void argsToDbFormat(){
        int orderId = 0;
        for (Lv1 item :items) {
            item.setOrderId(orderId++);
            ArrayList<Lv2> lv2 = item.getLv2();
            if (lv2.size()>1){
                ArrayList<HashMap<String,String>> lv2NamesList = new ArrayList<>();
                ArrayList<String> lv2MarksList = new ArrayList<>();
                for (Lv2 i :lv2) {
                    HashMap<String,String> th = new HashMap<>();
                    th.put("value",i.getLv2name());
                    lv2NamesList.add(th);
                    lv2MarksList.add(i.getLv2mark());
                }
                // 创建 ObjectMapper
                ObjectMapper objectMapper = new ObjectMapper();

                // 转换为 JSON 字符串
                String jsonString = null;
                try {
                    jsonString = objectMapper.writeValueAsString(lv2NamesList);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                item.setLvl2Names(jsonString);
                item.setLvl2Marks(String.join(",",lv2MarksList));

            }else {
                item.setLvl2Names(lv2.get(0).getLv2name());
                item.setLvl2Marks(lv2.get(0).getLv2mark());
            }
        }

    }

}
@Data
class Lv1{
    private String lv1name;
    private String lv1mark;
    private Integer orderId;
    private ArrayList<Lv2> lv2;
    private String lvl2Names;
    private String lvl2Marks;
}
@Data
class Lv2{
    private String lv2name;
    private String lv2mark;
}
